'''
Created on 2011-3-6

@author: bnw
'''
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
from stocktable import stocklist
import sys

if __name__ == '__main__':
##    reload(sys)
##    sys.setdefaultencoding('gbk')
    for stock in stocklist[1:2]:
        stockid = '000012'
##        print stockid
        if stockid != '000012':
            continue
        urlstr = "http://q.stock.sohu.com/cn/" + stockid + "/ltgd_2.shtml"
        page = urllib2.urlopen(urlstr)
        soup = BeautifulSoup(page)
        fpathstr = "F:/data/CirculateStockHolder/Sohu/" + stockid + ".txt"
        f = open(fpathstr, 'w')
        divs = soup.findAll("div", "part")
        newdivs = [] 
        for div in divs:
            if div.h4 != None:
                newdivs.append(div)
##        print newdivs
        for div in newdivs:
            print div.table('tr')[1]('td')[1].a.string.strip()
##        table = soup('table', id = 'CirculateShareholderTable')[0]
##        trs = table('tr')[1:]
##        time = ''
##        rank = ''
##        name = ''
##        quan = ''
##        perc = '' 
##        attr = ''
##        for i in range(len(trs)):
##            if i%14 == 0:
##                time = trs[i]('td')[1].string
##                continue
##            elif i%14 == 1 or i%14 == 2 or i%14==13:
##                continue
##            else:
####               print i
##                rank = trs[i]('td')[0].div.string.strip()
##                name = trs[i]('td')[1].div.string.strip()
##                print name.__class__
##                print isinstance(name, unicode)
##                try:
##                    name = name.encode('gbk')
##                except UnicodeEncodeError:
##                    name = 'XX'
##                finally:
##                    print name
##                print name
##                print sys.getdefaultencoding()
##                quan = trs[i]('td')[2].div.string.strip()    
##                perc = trs[i]('td')[3].div.string.strip()     
##                attr = trs[i]('td')[4].div.string.strip().encode('utf8')
##            print name, attr
##            f.write(time)
##            f.write('\t')
##            f.write(rank)
##            f.write('\t')
##            f.write(name)
##            f.write('\t')
##            f.write(quan)
##            f.write('\t')
##            f.write(perc)
##            f.write('\t')
##            f.write(attr)
##            f.write('\n')           
##        f.closed     
        
    
